package com.woniuxy.woniumall.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.woniuxy.woniumall.entity.User;
import com.woniuxy.woniumall.service.UserService;
import com.woniuxy.woniumall.vo.AccountVo;
import com.woniuxy.woniumall.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    DefaultKaptcha defaultKaptcha ;

    @Autowired
    UserService userService;

    @PostMapping("/login")
    @ResponseBody
    public ResponseResult<User> login(@RequestBody AccountVo accountVo, HttpServletRequest request){
        ResponseResult<User> result = new ResponseResult<>();
        try {
            String verifyCode = (String) request.getSession().getAttribute("verifyCode");
            Jedis jedis = new Jedis("127.0.0.1",6379);
            String verifyCode1 = jedis.get("verifyCode");
            if (verifyCode1 == null) {
                //过期
            }
            if (accountVo.getCheckCode().equalsIgnoreCase("111111")) {
                User user = userService.login(accountVo.getUsername());
                if (user == null) {
                    result.setCode(202);
                    result.setMsg("账户不存在 !");
                }else {
                    if (user.getPassword().equals(accountVo.getPassword())) {
                        //登录成功
                        request.getSession().setAttribute("userInfo",user);
                        result.setCode(200);
                        result.setMsg("登录成功！");
                        result.setData(user);
                    }else {
                        result.setCode(203);
                        result.setMsg("密码错误 !");
                    }
                }
            }else {
                result.setCode(201);
                result.setMsg("验证码不正确!");
            }
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(205);
            result.setMsg("系统错误，请联系管理员!");
        }
        return result ;
    }



















    @GetMapping("getVerifyCode")
    public void defaultKaptcha(HttpServletRequest request,
                               HttpServletResponse response) throws Exception {
        byte[] captchaOutputStream = null;
        ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream();
        try {
            //生产验证码字符串并保存到session中
            String verifyCode = defaultKaptcha.createText();
            //把验证码 存储到session中
            request.getSession().setAttribute("verifyCode", verifyCode);
            //存储到redis中  存储时间是 2分钟
            Jedis jedis = new Jedis("127.0.0.1",6379);
            jedis.setex("verifyCode",120,verifyCode);
            //this.setCheckCode(verifyCode);
            BufferedImage challenge = defaultKaptcha.createImage(verifyCode);
            ImageIO.write(challenge, "jpg", imgOutputStream);
        } catch (IllegalArgumentException e) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        captchaOutputStream = imgOutputStream.toByteArray();
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        ServletOutputStream responseOutputStream = response.getOutputStream();
        responseOutputStream.write(captchaOutputStream);
        responseOutputStream.flush();
        responseOutputStream.close();
    }



}
